﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;

namespace jnxc.Web.usercategory
{
    /// <summary>
    /// GetUsercate 的摘要说明
    /// </summary>
    public class GetUsercate : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.usercategory bll = new jnxc.BLL.usercategory();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "add")
                add(context);
            else if (ac == "delete")
                delete(context);
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string caid = context.Request.QueryString["caid"];
                if (caid != "")
                {
                    int id = int.Parse(caid);
                    jnxc.Model.usercategory model = new jnxc.Model.usercategory();
                    model.caid = id;
                    //model.uid = uid;
                    string[] h = context.Request.QueryString["uids"].Split(',');
                    int hid = 0;
                    int i = 0;
                    foreach (string hi in h)
                    {
                        hid = int.Parse(hi);
                        if (hid != 0)
                        {
                            model.uid = hid;
                            i = i + bll.Delete(model);
                        }
                    }
                    if (i > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string caid = context.Request.QueryString["caid"];
                if (caid != "")
                {
                    int id = int.Parse(caid);
                    jnxc.Model.usercategory model = new jnxc.Model.usercategory();
                    model.caid = id;
                    //model.uid = uid;
                    string[] h = context.Request.QueryString["uids"].Split(',');
                    int hid = 0;
                    int i = 0;
                    foreach (string hi in h)
                    {
                        hid = int.Parse(hi);
                        if (hid != 0)
                        {
                            model.uid = hid;
                            i = i + bll.Add(model);
                        }
                    }
                    if (i > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string  uprovince, ucity,  ucounty;
                uprovince = ucity =  ucounty = "";

                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                string uname, utruename, st,caid;
                uname = utruename = st = caid="";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["caid"])
                {//获取前台传来的值  
                    caid = context.Request.Form["caid"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" uname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", utruename);
                }
                if (caid != "")
                {
                    strWhere.AppendFormat(" caids like '%,{0},%' and ", caid);
                }
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }
                if (ucounty != "")
                {
                    strWhere.AppendFormat(" ucounty='{0}' and ", ucounty);
                }

                if (ucity != "")
                {
                    strWhere.AppendFormat(" ucity='{0}' and ", ucity);
                }

                if (uprovince != "")
                {
                    strWhere.AppendFormat(" uprovince='{0}' and ", uprovince);

                }
                strWhere.AppendFormat(" ulevel<{0} ", level);
                
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPagemy(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows,uid);
                int count = bll.GetRecordCountmy(strWhere.ToString(),uid);//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}